#coding=utf-8
import stutils

def get_ipo_from_soup(soup):
    t = unicode('上市日期：', 'u8')
    
    rq = soup.find(text=lambda x: x.find(t) !=  -1)
    if not rq:
        print "Can not from 上市日期： from the page."
        return None
    
    date = rq.parent.nextSibling.nextSibling.string
    if not date:
        print "no date;"
        return None
    date = date.encode('u8')
    return date

class NoIpoDateError(Exception):
    def __init__(self, code, site):
        self.code = code
        self.site = site
    def __str__(self):
        return "sorry, Can not find ipo date from %s of %s"%(self.code, self.site)
    
def get_ipo(code):
    '''获得上市公司的上市日期 from sina'''
    url = "http://money.finance.sina.com.cn/corp/go.php/vCI_CorpInfo/stockid/%s.phtml"%code
    
    s = stutils.get_soup(url)
    date = get_ipo_from_soup(s)
    if not date:
        raise NoIpoDateError(code, 'sina')
    return date

if __name__ == '__main__':
    from stsettings import datadir
    fn = datadir + '/ipo.csv'
    codes = stutils.codes_from_datafile()
    
    cis = []
    head = ['代码','上市日期']
    for c in codes:
        try:
            i = get_ipo(c)
        except NoIpoDateError,e:
            print e
            continue
        
        cis.append([c, i])
        print c
    
    stutils.data2csv(cis, head, fn)
    